package com.freesky.web.manager.controller.order;

import com.freesky.common.pojo.DataTablesResult;
import com.freesky.common.utils.reponse.JSONResponse;
import com.freesky.web.manager.remote.order.OrderRemote;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * @author Freesky
 */
@Controller
@Api(description = "订单管理")
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderRemote orderRemote;

    @GetMapping("/page")
    public String page() {
        return "order/list";
    }

    @GetMapping("/print/page")
    public String print() {
        return "order/print";
    }

    @GetMapping("/remark/page")
    public String remark() {
        return "order/remark";
    }

    @GetMapping("/deliver/page")
    public String deliver() {
        return "order/deliver";
    }

    @ResponseBody
    @GetMapping(value = "/list")
    @ApiOperation(value = "获取订单列表")
    public DataTablesResult getOrderList(
            @RequestParam(value = "draw") int draw,
            @RequestParam(value = "start") int start,
            @RequestParam(value = "length") int length,
            @RequestParam(value = "search[value]") String search,
            @RequestParam(value = "order[0][column]") int orderCol,
            @RequestParam(value = "order[0][dir]") String orderDir) {

        return orderRemote.getOrderList(draw, start, length, search, orderCol, orderDir);
    }

    @ResponseBody
    @GetMapping(value = "/count")
    @ApiOperation(value = "获取订单总数")
    public JSONResponse getOrderCount() {
        return orderRemote.getOrderCount();
    }

    @ResponseBody
    @GetMapping(value = "/detail/{orderId}")
    @ApiOperation(value = "获取订单详情")
    public JSONResponse getOrderDetail(@PathVariable(value = "orderId") String orderId) {
        return orderRemote.getOrderDetail(orderId);
    }

    @ResponseBody
    @PostMapping(value = "/remark")
    @ApiOperation(value = "订单备注")
    public JSONResponse remark(
            @RequestParam(value = "orderId") String orderId,
            @RequestParam(value = "message", required = false) String message) {
        return orderRemote.remark(orderId, message);
    }

    @ResponseBody
    @PostMapping(value = "/deliver")
    @ApiOperation(value = "订单发货")
    public JSONResponse deliver(
            @RequestParam(value = "orderId") String orderId,
            @RequestParam(value = "shippingName") String shippingName,
            @RequestParam(value = "shippingCode") String shippingCode,
            @RequestParam(value = "postFee") BigDecimal postFee) {

        return orderRemote.deliver(orderId, shippingName, shippingCode, postFee);
    }

    @ResponseBody
    @GetMapping(value = "/cancel/{orderId}")
    @ApiOperation(value = "订单取消")
    public JSONResponse cancelOrderByAdmin(@PathVariable(value = "orderId") String orderId) {

        return orderRemote.cancelOrderByAdmin(orderId);
    }

    @ResponseBody
    @DeleteMapping(value = "/del/{ids}")
    @ApiOperation(value = "删除订单")
    public JSONResponse delete(@PathVariable(value = "ids") String[] ids) {
        return orderRemote.delete(ids);
    }
}
